package com.example.myapplication.event

import android.app.Activity
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import java.util.*

class ScanBroadcastReceive : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        try {
            val action = intent.action
            var barcode: String? = ""
            Log.e(TAG, "onReceive:                $action")
            if (action == "ACTION_BAR_SCAN") {
                barcode = intent.getStringExtra("EXTRA_SCAN_DATA")
            } /*else if (action.equals("com.android.scancontext")) {
                barcode = intent.getStringExtra("Scan_context");

            } */ else if (action
                == "com.android.server.scannerservice.broadcast"
            ) {
                barcode = intent.getStringExtra("scannerdata")
            } else if (barcode == "" && action == "com.android.scanservice.scancontext") {
                barcode = intent.getStringExtra("Scan_context")
            } else {
                if (intent.hasExtra("value")) {
                    barcode = intent.getStringExtra("value")
                }
            }
            if (barcode!!.isEmpty()) {
                return
            }
            runPool!!.add(barcode)
        } catch (ee: Exception) {
            ee.printStackTrace()
        }
    }

    fun returnBarcode(barcode: String?) {
        val iter: Iterator<*> = mHandler.entries.iterator()
        while (iter.hasNext()) {
            val entry = iter.next() as Map.Entry<*, *>
            if (entry.key != null) {
                val key = entry.key as Activity
                if (!key.isFinishing) {
                    if (entry.value != null) {
                        val msg = Message()
                        msg.what = 100
                        msg.data = Bundle()
                        msg.data.putString("BARCODE", barcode)
                        (entry.value as Handler).sendMessage(msg)
                    }
                }
            }
        }
    }

    companion object {
        private const val TAG = "ScanBroadcastReceive"
        // public static final String ACTION_BAR_SCAN = "ACTION_BAR_SCAN";
        // public static final String EXTRA_SCAN_DATA="EXTRA_SCAN_DATA";

        var runPool: Queue<String?>? = null
        var runThid: Thread? = null
        var mHandler = HashMap<Activity, Handler?>()

        fun setScanHandler(who: Activity, scanHandler: Handler?) {
            if (mHandler[who] != null) {
                mHandler.remove(who)
            }
            if (scanHandler != null) {
                mHandler[who] = scanHandler
            }
        }
    }

    init {
        if (runThid == null) {
            runPool = LinkedList()
        }
        if (runThid == null) {
            runThid = object : Thread() {
                override fun run() {
                    // TODO Auto-generated method stub
                    while (true) {
                        try {
                            sleep(100)
                        } catch (e: InterruptedException) {
                            // TODO Auto-generated catch block
                            e.printStackTrace()
                        }
                        if (runPool!!.size > 0) {
                            returnBarcode(runPool!!.poll())
                        }
                    }
                }
            }
            if (runThid != null) {
                runThid!!.start()
            }
        }
    }
}